package com.dyzx.hbb.modules.wms.controller;


import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.wms.entity.WmsLocationArea;
import com.dyzx.hbb.modules.wms.entity.WmsProductCategory;
import com.dyzx.hbb.modules.wms.query.WmsProductCategoryQuery;
import com.dyzx.hbb.modules.wms.service.WmsProductCategoryService;
import com.dyzx.hbb.modules.wms.vo.WmsProductCategoryUVO;
import com.dyzx.hbb.modules.wms.vo.WmsProductCategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 产品类别管理控制器
 * <p>处理仓库管理系统中产品类别的CRUD操作和相关业务逻辑
 * <p>主要职责：
 * <ul>
 * <li>提供产品类别的增删改查接口</li>
 * <li>支持按条件查询产品类别列表</li>
 * <li>提供树形结构的产品类别列表</li>
 * <li>实现简易产品类别列表查询</li>
 * <li>处理产品类别的父子关系验证</li>
 * </ul>
 * <p>设计意图：为仓库管理模块提供产品分类的标准化管理，
 * 便于产品的分类存储和检索
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "产品类别管理", description = "产品类别相关接口")
@RestController
@RequestMapping("/wms/product/category")
public class WmsProductCategoryController extends BaseValidController<
        WmsProductCategoryService,
        WmsProductCategory,
        WmsProductCategoryQuery,
        WmsProductCategoryVO,
        WmsProductCategoryUVO
        > {

    @Autowired
    private WmsProductCategoryService service;

    @Override
    protected WmsProductCategoryService getBaseService() {
        return service;
    }
    @Operation(summary = "分页查询")
    @GetMapping("/tree_list")
    public Result<List<Map<String, Object>>> list() {
        List<Map<String, Object>> list=service.findByTreeList();
        return Result.ok(list);
    }
    @Override
    protected  MyLambdaQueryWrapper<WmsProductCategory> queryWrapper(WmsProductCategoryQuery query) {
        MyLambdaQueryWrapper<WmsProductCategory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(WmsProductCategory::getId, WmsProductCategory::getName, WmsProductCategory::getParentId);
        queryWrapper.orderByDesc(WmsProductCategory::getCreateDate);

        if (query != null) {
            queryWrapper.likeRight(query.getName() != null, WmsProductCategory::getName, query.getName())
                    .eq(query.getParent_id() != null, WmsProductCategory::getParentId, query.getParent_id())
         ;
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsProductCategory> queryInfoWrapper() {
        MyLambdaQueryWrapper<WmsProductCategory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmsProductCategory::getCreateDate);
        return queryWrapper;
    }

    @Override
    protected WmsProductCategory addValid(WmsProductCategoryVO entity) {
        Long parentId = entity.getParentId();
        if (parentId != null) {
            WmsProductCategory byId = service.getById(parentId);
            if (byId == null) {
          throw    new RuntimeException("父级不存在");
            }
        }else {
            entity.setParentId(null);
        }
        WmsProductCategory wmsProductCategory = new WmsProductCategory();
        BeanUtil.copyProperties(entity, wmsProductCategory);

        return wmsProductCategory;
    }

    @Override
    protected WmsProductCategory editValid(WmsProductCategoryUVO entity) {
        return null;
    }

    @Operation(summary = "简易列表")
    @GetMapping("/mini_list")
    public Result<List<Map<String, Object>>> miniList(
    ) {


        List<Map<String, Object>> list = service.findByMiniList();
        return list.isEmpty() ? Result.error(404, "暂无数据") : Result.ok(list);
    }
}